package com.Endurancer.manager.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.Endurancer.common.constant.Constants;
import com.Endurancer.common.core.domain.entity.SysUser;
import com.Endurancer.common.core.domain.model.LoginBody;
import com.Endurancer.common.utils.MessageUtils;
import com.Endurancer.common.utils.SecurityUtils;
import com.Endurancer.framework.manager.AsyncManager;
import com.Endurancer.framework.manager.factory.AsyncFactory;
import com.Endurancer.framework.web.service.SysLoginService;
import com.Endurancer.manager.domain.SysUserRole;
import com.Endurancer.manager.domain.dto.UserDto;
import com.Endurancer.manager.domain.vo.UserVo;
import com.Endurancer.manager.service.ISysUserService;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.Endurancer.common.annotation.Log;
import com.Endurancer.common.core.controller.BaseController;
import com.Endurancer.common.core.domain.AjaxResult;
import com.Endurancer.common.enums.BusinessType;
import com.Endurancer.manager.domain.User;
import com.Endurancer.manager.service.IUserService;
import com.Endurancer.common.utils.poi.ExcelUtil;
import com.Endurancer.common.core.page.TableDataInfo;

/**
 * 用户信息Controller
 *
 * @author Endurancer
 * @date 2025-02-02
 */
@RestController
@RequestMapping("/manager/user")
public class UserController extends BaseController
{
    @Autowired
    private IUserService userService;

    @Autowired
    private SysLoginService loginService;
    @Autowired
    private ISysUserService sysUserService;

    /**
     * 登录方法
     *
     * @param loginBody 登录信息
     * @return 结果
     */
    @PostMapping("/login")
    public AjaxResult login(@RequestBody LoginBody loginBody)
    {
        AjaxResult ajax = AjaxResult.success();
        // 生成令牌
        String token = loginService.login(loginBody.getUsername(), loginBody.getPassword(), loginBody.getCode(),
                loginBody.getUuid());
        ajax.put(Constants.TOKEN, token);
        SysUser sysUser = sysUserService.selectUserByUserName(loginBody.getUsername());
        User user = userService.selectUserBySysUserId(sysUser);
        if (user != null) {
            Map<String, Object> userInfo = new HashMap<>();
            userInfo.put("userId", user.getUserId());
            userInfo.put("nickName", user.getNickName());
            userInfo.put("avatar", user.getAvatar());
            // 创建一个data对象，并设置其值为flag
            Map<String, Object> data = new HashMap<>();
            // 将userInfo和data都放入ajax对象中
            ajax.put("user", userInfo);
        } else {
            return AjaxResult.error("用户信息查询失败");
        }
        return ajax;
    }

    /**
     * 注册用户
     */
    @ApiOperation("注册用户")
    @Log(title = "用户", businessType = BusinessType.INSERT)
    @PostMapping("/register")
    public AjaxResult register(@RequestBody UserDto userDto ) {
        return success(userService.AppRegister(userDto));
    }

    /**
     * 获取用户信息详细信息
     */
    @GetMapping(value = "/getInfo/{userId}")
    public AjaxResult getPerson(@PathVariable("userId") Long userId)
    {
        return success(userService.getPerson(userId));
    }
    /**
     * 修改用户信息
     */
    @Log(title = "用户信息", businessType = BusinessType.UPDATE)
    @PutMapping("/edit")
    public AjaxResult editPerson(@RequestBody UserVo userVo)
    {
        return toAjax(userService.editPerson(userVo));
    }

    /**
     * 查询用户信息列表
     */
    @PreAuthorize("@ss.hasPermi('manager:user:list')")
    @GetMapping("/list")
    public TableDataInfo list(User user)
    {
        startPage();
        List<User> list = userService.selectUserList(user);
        return getDataTable(list);
    }

    /**
     * 导出用户信息列表
     */
    @PreAuthorize("@ss.hasPermi('manager:user:export')")
    @Log(title = "用户信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, User user)
    {
        List<User> list = userService.selectUserList(user);
        ExcelUtil<User> util = new ExcelUtil<User>(User.class);
        util.exportExcel(response, list, "用户信息数据");
    }

    /**
     * 获取用户信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('manager:user:query')")
    @GetMapping(value = "/{userId}")
    public AjaxResult getInfo(@PathVariable("userId") Long userId)
    {
        return success(userService.selectUserByUserId(userId));
    }

    /**
     * 新增用户信息
     */
    @PreAuthorize("@ss.hasPermi('manager:user:add')")
    @Log(title = "用户信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody User user)
    {
        return toAjax(userService.insertUser(user));
    }

    /**
     * 修改用户信息
     */
    @PreAuthorize("@ss.hasPermi('manager:user:edit')")
    @Log(title = "用户信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody User user)
    {
        return toAjax(userService.updateUser(user));
    }

    /**
     * 删除用户信息
     */
    @PreAuthorize("@ss.hasPermi('manager:user:remove')")
    @Log(title = "用户信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{userIds}")
    public AjaxResult remove(@PathVariable Long[] userIds)
    {
        return toAjax(userService.deleteUserByUserIds(userIds));
    }
}
